﻿namespace com.stifel.ServerBuildMgmt
{
    using System.Linq;
    using System.Reflection;
    using System.Xml;

    using com.stifel;
    using com.stifel.Extensions.AssemblyExtensions;
    using com.stifel.Extensions.SymbolExtensions;
    using com.stifel.ServerBuildMgmtDao;
    using com.stifel.SharePoint;

    using Microsoft.SharePoint;

    public class StifelVirtualServerLocation_Definition : BaseDefinition
    {
        #region Fields

        const string SITECOLUMNS = "Stifel-VirtualServerLocation.xml";

        #endregion Fields

        #region Constructors

        public StifelVirtualServerLocation_Definition( SPFeatureReceiverProperties properties )
            : base(properties)
        {
        }

        ~StifelVirtualServerLocation_Definition( )
        {
            Dispose( false );
        }

        #endregion Constructors

        #region Methods

        public override void Create( )
        {
            base.Create( );
            CreateSiteColumns( getFieldDefinitionsAsXml( SITECOLUMNS ) );
            CreateContentType( getContentTypeDefn( ) );
            CreateList( getListDefn( ) );
            loadList( );
        }

        public override void Delete( )
        {
            base.Delete( );
            DeleteList( getListDefn( ), false );
            DeleteContentType( getContentTypeDefn( ) );
            DeleteSiteColumns( getFieldDefinitionsAsXml( SITECOLUMNS ) );
        }

        ContentTypeDefn getContentTypeDefn( )
        {
            StifelVirtualServerLocation c = null;
            return new ContentTypeDefn
            {
                Name = Item.GetContentTypeName<StifelVirtualServerLocation>( ),
                ContentTypeId = Item.GetContentTypeId<StifelVirtualServerLocation>( ),
                WebLevel = true,
                Group = "Stifel",
                Description = "Describes a virtual server location.",
                Fields = new FieldDefn[ ] {
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerLocation>(c.GetPropertySymbol(o=>o.TimeZoneLookupId)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerLocation>(c.GetPropertySymbol(o=>o.USStatesLookupId)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerLocation>(c.GetPropertySymbol(o=>o.DNS)),
                        Required=true
                    },
                    new FieldDefn{
                        Name=Item.GetColumnName<StifelVirtualServerLocation>(c.GetPropertySymbol(o=>o.WINS)),
                        Required=true
                    },
                }.Distinct( ).ToList( )
            };
        }

        XmlNodeList getFieldDefinitionsAsXml( string resource )
        {
            XmlNodeList fields = null;
            string s = Assembly.GetExecutingAssembly( ).GetFullyQualifiedManifestResourceName( resource );
            string xml = Assembly.GetExecutingAssembly( ).GetManifestResourceAsString( s );
            if ( !string.IsNullOrEmpty( xml ) )
            {
                XmlDocument d = new XmlDocument( );
                d.LoadXml( xml );
                XmlNode n = d.SelectSingleNode( "Fields" );
                fields = n.SelectNodes( "Field" );
            }
            return fields;
        }

        ListDefn getListDefn( )
        {
            return new ListDefn
            {
                RootFolderName = StifelVirtualServerLocationDao.ListName,
                Title = "Stifel Virtual Server Location",
                Description = "Contains the available virtual server locations.",
                ContentTypes = new string[ ] {
                    Item.GetContentTypeName<StifelVirtualServerLocation>( )
                }.ToList( )
            };
        }

        int getTimezone( string value )
        {
            com.stifel.GlobalDao.TimezoneDao dao = new com.stifel.GlobalDao.TimezoneDao( );
            com.stifel.GlobalDao.Timezone o = dao.Get( value );
            return o != null && o.Id.HasValue ? o.Id.Value : 0;
        }

        int getUSSState( string abbreviation )
        {
            com.stifel.GlobalDao.USStatesDao dao = new com.stifel.GlobalDao.USStatesDao( );
            com.stifel.GlobalDao.USStates o = dao.Get( abbreviation );
            return o != null && o.Id.HasValue ? o.Id.Value : 0;
        }

        void loadList( )
        {
            StifelVirtualServerLocationDao dao = new StifelVirtualServerLocationDao( );
            dao.Clear( );
            dao.Add( new StifelVirtualServerLocation
            {
                Title = "Jersey City",
                USStatesLookupId = getUSSState( "NJ" ),
                TimeZoneLookupId = getTimezone( TimeZoneInfoConstants.EasternStandardTime ),
                DNS = "172.23.34.12; 172.23.34.10; 172.22.34.10;",
                WINS = "172.23.34.10; 172.22.34.10;"
            } );
            dao.Add( new StifelVirtualServerLocation
            {
                Title = "St Louis",
                USStatesLookupId = getUSSState( "MO" ),
                TimeZoneLookupId = getTimezone( TimeZoneInfoConstants.CentralAmericaStandardTime ),
                DNS = "172.22.34.12; 172.22.34.10; 172.23.34.10;",
                WINS = "172.22.34.10; 172.23.34.10;"
            } );
            dao.Add( new StifelVirtualServerLocation
            {
                Title = "San Francisco",
                USStatesLookupId = getUSSState( "CA" ),
                TimeZoneLookupId = getTimezone( TimeZoneInfoConstants.PacificStandardTime ),
                DNS = "172.17.136.11; 172.17.136.12; 172.22.34.10;",
                WINS = "172.22.34.10; 172.23.34.10;"
            } );
            dao.Add( new StifelVirtualServerLocation
            {
                Title = "Baltimore",
                USStatesLookupId = getUSSState( "MD" ),
                TimeZoneLookupId = getTimezone( TimeZoneInfoConstants.EasternStandardTime ),
                DNS = "",
                WINS = ""
            } );
        }

        #endregion Methods
    }
}